<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>go标准包：fmt</title>
  
    <meta name="author" content="秀才">

    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le styles -->
    <link href="/assets/twitter/stylesheets/bootstrap.min.css" type="text/css" rel="stylesheet" media="all">
<link href="/assets/twitter/stylesheets/style.css" type="text/css" rel="stylesheet" media="all">
 
    <link href="/assets/twitter/widgets/google_prettify/stylesheets/twitter-bootstrap.css" type="text/css" rel="stylesheet" media="all">

    <!-- Le fav and touch icons -->
  <!-- Update these with your own images
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
  -->
  </head>

  <body>

    <div class="navbar">
      <div class="navbar-inner">
        <div class="container">
          <a class="brand" href="/">秀才</a>
          <ul class="nav">
            
              


  <li><a href="/tags">标签</a></li>


            
              


  <li><a href="/categories">分类</a></li>


            
              


  <li><a href="/about">关于我</a></li>


            
          </ul>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="content">
        <div class="page-header">
  <h1>go标准包：fmt </h1>
</div>

<div class="row">
  <div class="span12">
    <h3>打印带颜色log</h3>

<p>golang打印log的颜色，原理上和linux终端下输出带颜色的文字是一样的</p>

<p><strong>格式很简单：</strong></p>

<pre><code class="language-shell">\033[显示方式;前景色;背景色m
</code></pre>

<p><strong>参数：</strong></p>

<table>
<thead>
<tr>
<th>显示方式</th>
<th>意义</th>
</tr>
</thead>

<tbody>
<tr>
<td>0</td>
<td>终端默认设置</td>
</tr>

<tr>
<td>1</td>
<td>高亮显示</td>
</tr>

<tr>
<td>4</td>
<td>使用下划线</td>
</tr>

<tr>
<td>5</td>
<td>闪烁</td>
</tr>

<tr>
<td>7</td>
<td>反白显示</td>
</tr>

<tr>
<td>8</td>
<td>不可见</td>
</tr>
</tbody>
</table>

<table>
<thead>
<tr>
<th>前景色</th>
<th>背景色</th>
<th>颜色</th>
</tr>
</thead>

<tbody>
<tr>
<td>30</td>
<td>40</td>
<td>黑色</td>
</tr>

<tr>
<td>31</td>
<td>41</td>
<td>红色</td>
</tr>

<tr>
<td>32</td>
<td>42</td>
<td>绿色</td>
</tr>

<tr>
<td>33</td>
<td>43</td>
<td>黃色</td>
</tr>

<tr>
<td>34</td>
<td>44</td>
<td>蓝色</td>
</tr>

<tr>
<td>35</td>
<td>45</td>
<td>紫红色</td>
</tr>

<tr>
<td>36</td>
<td>46</td>
<td>青蓝色</td>
</tr>

<tr>
<td>37</td>
<td>47</td>
<td>白色</td>
</tr>
</tbody>
</table>

<p><strong>示例</strong></p>

<ul>
<li>只是想简单设置文字颜色</li>
</ul>

<pre><code class="language-go">  fmt.Printf(&quot;\033[31m红色文字\033[0m &quot;)
  fmt.Printf(&quot;\033[32m绿色文字\033[0m &quot;)
  fmt.Printf(&quot;\033[33m黄色文字\033[0m &quot;)
</code></pre>

<ul>
<li>更加详细的颜色设置</li>
</ul>

<pre><code class="language-go">  fmt.Printf(&quot;\033[1;31;40m红色文字\033[0m &quot;)
</code></pre>

<ul>
<li>当然，好多博客都是遍历颜色供大家挑选，此处借用一下代码</li>
</ul>

<pre><code class="language-go">  for d := range []int{0, 1, 4, 5, 7, 8} { // 显示方式 = 0,1,4,5,7,8
  	for f := 30; f &lt;= 37; f++ { // 前景色彩 = 30-37
  		for b := 40; b &lt;= 47; b++ { // 背景色彩 = 40-47
  			key := fmt.Sprintf(&quot;%d;%d;%d&quot;, d, f, b)
  			fmt.Printf(&quot; %c[%sm%s%c[0m &quot;, 0x1B, key, key, 0x1B)
  		}
  		fmt.Println(&quot;&quot;)
  	}
  	fmt.Println(&quot;&quot;)
  }
</code></pre>

    <hr>
    <div class="pagination">
      <ul>
        <ul>
          
            <li class="prev"><a href="/go/%E5%AD%97%E5%85%B8%E6%A0%91---GO%E7%9A%84%E5%AE%9E%E7%8E%B0/" title="字典树 - GO的实现">&larr; Previous</a></li>
          
          

            <li><a href="/archive">Archive</a></li>

          
            <li class="next"><a href="/go/go%E6%A0%87%E5%87%86%E5%8C%85%EF%BC%9Acontext/" title="go标准包：context">Next &rarr;</a></li>
          
          
        </ul>
      </ul>
    </div>
    <hr>
    
  </div>
  
  <!-- <div class="span4">
    <h4>Published</h4>
    <div class="date"><span>2016-12-19</span></div>
    <br>
    <h4>Categories</h4>
    <ul class="tag_box">
    
      <li>
  <a href="/categories/#go-ref">go <span>7</span></a>
</li>
    
    </ul>
    <br>
    <h4>Tags</h4>
    <ul class="tag_box">
    
      <li>
  <a href="/tags/#go-ref">go <span>7</span></a>
</li>
    
      <li>
  <a href="/tags/#log-ref">log <span>1</span></a>
</li>
    
    </ul>
  </div> -->
</div>

      </div>

      <footer>
        <p>&copy; 秀才 2013 
          with help from <a href="http://github.com/wendal/gor" target="_blank" title="Gor -- Fast Blog">Gor</a>
          and <a href="http://twitter.github.com/bootstrap/" target="_blank">Twitter Bootstrap</a>
		  and Idea from <a href="http://ruhoh.com" target="_blank" title="The Definitive Technical Blogging Framework">ruhoh</a>
        </p>
      </footer>

    </div> <!-- /container -->
    <!-- <script src="//cdnjscn.b0.upaiyun.com/libs/prettify/r298/prettify.min.js"></script> -->
    <script src="http://cdn.staticfile.org/prettify/r298/prettify.min.js"></script>
    <script>
      var pres = document.getElementsByTagName("pre");
      for (var i=0; i < pres.length; ++i) {
        pres[i].className = "prettyprint linenums";
      }
      prettyPrint();
    </script>
    
    
  </body>
</html>
